package com.laomo.lib_bt;

import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.os.Handler;
import android.util.Log;

import com.laomo.lib_bt.callback.ConnectCallbackInterface;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：连接事件分发
 *
 * @author laomo
 * @date 2025/4/22 14:32
 */
public class ConnectEventUtil implements ConnectCallbackInterface {
    //连接状态监听
    private List<ConnectCallbackInterface> mConnectCallbackList = new ArrayList<>();
    private Handler mHandler = new Handler();

    //连接超时时间
    private static final int CONNECT_TIMEOUT = 8000;

    private Runnable mConnectTimeoutRunnable = new Runnable() {
        @Override
        public void run() {
            onConnectFailed(mAddress, mDevice);
        }
    };
    private String mAddress;

    private long mConnectStartTime;
    private BluetoothDevice mDevice;

    public void addConnectCallbackListener(ConnectCallbackInterface connectCallbackInterface) {
        if (!mConnectCallbackList.contains(connectCallbackInterface)) {
            mConnectCallbackList.add(connectCallbackInterface);
        }
    }

    public void removeConnectCallbackListener(ConnectCallbackInterface connectCallbackInterface) {
        mConnectCallbackList.remove(connectCallbackInterface);
    }

    @Override
    public void onConnectSuccess(String address, BluetoothGatt gatt) {
        //打印连接耗时
        Log.d("BleManager", "onConnectSuccess: 连接耗时：" + (System.currentTimeMillis() - mConnectStartTime));
        mHandler.removeCallbacks(mConnectTimeoutRunnable);
        for (ConnectCallbackInterface connectCallbackInterface : mConnectCallbackList) {
            connectCallbackInterface.onConnectSuccess(address, gatt);
        }
    }

    @Override
    public void onConnectFailed(String address, BluetoothDevice device) {
        Log.d("BleManager", "onConnectFailed: 连接耗时：" + (System.currentTimeMillis() - mConnectStartTime));
        mHandler.removeCallbacks(mConnectTimeoutRunnable);
        for (ConnectCallbackInterface connectCallbackInterface : mConnectCallbackList) {
            connectCallbackInterface.onConnectFailed(address, device);
        }
    }

    @Override
    public void onDisConnect(String address, BluetoothGatt gatt) {
        for (ConnectCallbackInterface connectCallbackInterface : mConnectCallbackList) {
            connectCallbackInterface.onDisConnect(address, gatt);
        }
    }

    @Override
    public void onConnecting(String address, BluetoothDevice device) {
        mDevice = device;
        mConnectStartTime = System.currentTimeMillis();
        mAddress = address;
        mHandler.removeCallbacks(mConnectTimeoutRunnable);
        mHandler.postDelayed(mConnectTimeoutRunnable, CONNECT_TIMEOUT);
        for (ConnectCallbackInterface connectCallbackInterface : mConnectCallbackList) {
            connectCallbackInterface.onConnecting(address, device);
        }
    }

    @Override
    public void onMtuChanged(String address, BluetoothGatt gatt, int mtu, int status) {
         for (ConnectCallbackInterface connectCallbackInterface : mConnectCallbackList) {
             connectCallbackInterface.onMtuChanged(address, gatt, mtu, status);
         }
    }
}
